package com.devshephard.nio.nettybasic.future;

import io.netty.channel.DefaultEventLoop;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class PromiseFuture {
    public static void main(String[] args) {
        DefaultEventLoop eventExecutors = new DefaultEventLoop();
        DefaultPromise<Integer> promise = new DefaultPromise<>(eventExecutors);

// 设置回调，异步接收结果
        /*promise.addListener(future -> {
            // 这里的 future 就是上面的 promise
            System.out.println(Thread.currentThread().getName() + " " + future.getNow());
        });*/
        promise.addListener(promise1 -> {
            // 这里的 future 就是上面的 promise
            System.out.println(Thread.currentThread().getName() + " " + promise1.getNow());
        });


// 等待 1000 后设置成功结果
        eventExecutors.execute(()->{
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " "+ "set success, 10");
            promise.setSuccess(10);
        });

        System.out.println("start...");

    }
}
